﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WI.CandidateManager.Common.Data.Entities;
using WI.CandidateManager.Common.Data.Enums;

namespace WI.CandidateManager.Business.Objects
{
	public class OccurrenceBO:BOBase
	{
		#region DeleteAndGetOccurrence

		/// <summary>
		/// Deletes the occurence.
		/// </summary>
		/// <param name="occurrenceId">The occurrence id.</param>
		/// <returns></returns>
		public bool DeleteOccurence(long occurrenceId)
		{
			try
			{
				Logger.Info(String.Format("Try to delete occurrence (occurrenceId: {0})", occurrenceId));
				var occurrence = DataContext.OccurrenceSet.Single(o => o.Id == occurrenceId);
				occurrence.State = State.Deleted;
				DataContext.SaveChanges();
				return true;
			}
			catch (Exception ex)
			{
				Logger.Error(String.Format("Can't delete occurrence (occurrenceId: {0})", occurrenceId), ex);
				return false;
			}
		}

		/// <summary>
		/// Gets the occurrences.
		/// </summary>
		/// <param name="occurrenceId">The occurrence id.</param>
		/// <param name="subjectId">The subject id.</param>
		/// <param name="groupId">The group id.</param>
		/// <returns></returns>
		public IEnumerable<Occurrence> GetOccurrences(long? occurrenceId = null, long? subjectId=null, long? groupId = null)
		{
			return DataContext.OccurrenceSet.Where(o =>
				(!occurrenceId.HasValue || o.Id == occurrenceId) &&
				o.State == State.Active &&
				(!subjectId.HasValue || o.Schedule.SubjectId == subjectId) &&
				(!groupId.HasValue || o.Schedule.StudyGroupId == groupId));
		}

		#endregion

		#region SaveOccurrence

		/// <summary>
		/// Saves the occurence.
		/// </summary>
		/// <param name="occurrence">The occurrence.</param>
		/// <returns></returns>
		public bool SaveOccurence(Occurrence occurrence)
		{
			try
			{
				Logger.Info("Try to save occurrence");
				if (occurrence.Id > 0)
				{
					Logger.Info("Try to update occurrence");
					ApplyChanges(occurrence);
				}
				else
				{
					Logger.Info("Try to create occurrence");
					DataContext.OccurrenceSet.Add(occurrence);
				}

				DataContext.SaveChanges();
				return true;
			}
			catch (Exception ex)
			{
				Logger.Error("Can't save occurrence)", ex);
				return false;
			}
		}

		/// <summary>
		/// Applies the changes.
		/// </summary>
		/// <param name="occurrence">The occurrence.</param>
		/// <returns></returns>
		/// <exception cref="System.ArgumentNullException">occurrence</exception>
		protected Occurrence ApplyChanges(Occurrence occurrence)
		{
			try
			{
				Logger.Info("Try to apply changes for occurrence");
				if (occurrence == null)
				{
					throw new ArgumentNullException("occurrence");
				}

				var updateOccurrence = DataContext.OccurrenceSet.Single(o => o.Id == occurrence.Id);
				updateOccurrence.From = occurrence.From;
				updateOccurrence.To = occurrence.To;
				DataContext.Entry(updateOccurrence).State = EntityState.Modified;
				DataContext.SaveChanges();
				return updateOccurrence;
			}
			catch (Exception ex)
			{
				Logger.Error("Can't update occurrence)", ex);
				return new Occurrence();
			}
		}

		#endregion
	}
}
